package de.lns.tick;

import java.util.ArrayList;

import de.lns.level.Level;
import de.lns.render.RenderQueue;
import de.lns.render.Renderable;
import de.lns.ui.keyinput.Inputhandler;

@SuppressWarnings("unchecked")
public class GameState extends State {
	//Class variable initialization.
	private static GameState active;
	static{
		ArrayList<Renderable> list = new ArrayList<>();
		list.add(Level.getMap());
		list.add(new RenderQueue((Iterable<Renderable>)(Iterable<?>)Level.getEntityList()));
		list.add(Inputhandler.getInstance());
		
		RenderQueue queue = new RenderQueue(list);
		active = new GameState(queue);
	}
	
	//Instance variable initialization
	private long timeStart = 0;
	private long time = 0;
	private long ticks = 0;
	
	
	/**
	 * Hidden constructor, since
	 * this class uses the singleton pattern.
	 */
	private GameState(RenderQueue queue) {
		super(queue);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void update() {
		time = UpdateThread.getTime() - timeStart;
		//System.out.print(ticks++);
		//System.out.print(" +");
		//System.out.print(UpdateThread.getDelta());
		//System.out.print("ms @ ");
		//System.out.println(time);
		Level.active().update();
	}

	@Override
	protected void activated() {
		timeStart = UpdateThread.getTime(); 
	}
	
	/**
	 * This method returns the time (in ms) that has passed, <br>
	 * since this has become the active State.
	 * @return - the time, that Game has been active.
	 */
	public long time() {
		return time;
	}
	
	public static GameState active(){
		if(active == null)
			System.out.println("Derp");
		return active;
	}

	@Override
	public void mouse(MouseEvent m) {
		//Overlay.mouse(m);
		Inputhandler.getInstance().mouse(m);
	}

	@Override
	public void key(KeyEvent k) {
		//Overlay.key(k);
		Inputhandler.getInstance().key(k);
	}
	
}
